﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    showHostIp();
    ui->pushButton_listen->setShortcut(Qt::Key_Return);
    setWindowTitle("服务器端");
    ui->lineEdit_port->setText("8090");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showHostIp()
{
    QString localHostName = QHostInfo::localHostName();
    QHostInfo info = QHostInfo::fromName(localHostName);
    QList<QHostAddress> list = info.addresses();
    QString ip;
    for(int i = 0;i < list.size();i++)
    {
        if(list[i].protocol() == QAbstractSocket::IPv4Protocol)
        {
            ip+="*";
            ip+=list[i].toString();
        }
    }
    ui->label_ip->setText(ip);
}

void MainWindow::on_pushButton_listen_clicked()
{
    int port = ui->lineEdit_port->text().toInt();
    tcpServer = new MySocket(port);
    connect(tcpServer,SIGNAL(ReceiveMsg_peerAddress(QString)),this,SLOT(get_peerAddress(QString)));
    connect(tcpServer,SIGNAL(show_user_pass(QString,QString,QString)),
                                            this,
                                            SLOT(deal_Msg(QString , QString , QString )));
    ui->pushButton_listen->setEnabled(false);
}

void MainWindow::get_peerAddress(QString s)
{
    ui->listWidget->addItem(s);
}

void MainWindow::deal_Msg(QString type, QString user, QString password)
{
   qDebug()<<type<<user<<password;
   ui->textBrowser->append(type);
   ui->textBrowser->append(user);
   ui->textBrowser->append(password);
}
